//面试题 01.06. 字符串压缩

char* compressString(char* S){
    int len = strlen(S);
    if(len <= 2) return S;
    
    char* str = (char*)malloc((2*len)*sizeof(char));
    int count = 1, index = 0;
    for(int i=1; i<len+1; i++)
    {
        if(S[i] == S[i-1])
        {
            count++;
        }
        else
        {
            str[index++] = S[i-1];
            int wlen = sprintf(&str[index], "%d", count);
            index += wlen;
            count = 1;
        }
    }
    str[index] = '\0';
    if(index-1 >= len) return S;
    
    return str;
}